package com.ruoyi.dataMigration.service.impl;

import com.ruoyi.area.domain.HouseAreaExportT;
import com.ruoyi.area.domain.HouseAreaT;
import com.ruoyi.area.mapper.HouseAreaTMapper;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.data.DataUtils;
import com.ruoyi.dataMigration.domain.DoorDataT;
import com.ruoyi.dataMigration.domain.ProjectDataT;
import com.ruoyi.dataMigration.domain.RepairDataT;
import com.ruoyi.dataMigration.mapper.DataMigrationTMapper;
import com.ruoyi.dataMigration.service.IDataMigrationTService;
import com.ruoyi.door.domain.HouseDoorT;
import com.ruoyi.door.mapper.HouseDoorTMapper;
import com.ruoyi.door.service.IHouseDoorTService;
import com.ruoyi.group.domain.HouseGroupT;
import com.ruoyi.group.mapper.HouseGroupTMapper;
import com.ruoyi.purpose.domain.HousePurposeT;
import com.ruoyi.purpose.mapper.HousePurposeTMapper;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.unit.domain.HouseUnitT;
import com.ruoyi.unit.mapper.HouseUnitTMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 户管理Service业务层处理
 * 
 * @author lijie
 * @date 2023-04-20
 */
@Service
public class DataMigrationTServiceImpl implements IDataMigrationTService
{
    @Autowired
    private HouseDoorTMapper houseDoorTMapper;
    @Autowired
    private DataMigrationTMapper dataMigrationTMapper;
    @Autowired
    private HousePurposeTMapper housePurposeTMapper;
    @Autowired
    private HouseAreaTMapper houseAreaTMapper;
    @Autowired
    private HouseGroupTMapper houseGroupTMapper;
    @Autowired
    private HouseUnitTMapper houseUnitTMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Transactional
    @Override
    public String importUser(List<DoorDataT> userList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 1;
        int num = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        HouseDoorT houseDoorVo = new HouseDoorT();
        HousePurposeT housePurposeT = new HousePurposeT();
        HouseAreaT houseAreaT = new HouseAreaT();
        HouseGroupT houseGroupT = new HouseGroupT();
        HouseUnitT houseUnitT = new HouseUnitT();
        List<HouseDoorT> doorList = new ArrayList<HouseDoorT>();
        List<DoorDataT> doorList1 = new ArrayList<DoorDataT>();
        List<HouseAreaExportT> areaList = new ArrayList<HouseAreaExportT>();
        List<HouseGroupT> groupList = new ArrayList<HouseGroupT>();
        List<HouseUnitT> unitList = new ArrayList<HouseUnitT>();
        List<HousePurposeT> purposeList = new ArrayList<HousePurposeT>();
        purposeList = housePurposeTMapper.selectHousePurposeTList(housePurposeT);
        loop:
        for (DoorDataT user : userList)
        {
            int id = houseDoorTMapper.getDoorId();
            num ++;
            user.setCreateBy(operName);
            if(user.getCostDate()==null){
                user.setCostDate(DateUtils.dateTime("20230101","yyyy-mm-dd"));
            }
            if(user.getAreaName().isEmpty()){
                failureNum++;
                String returnMsg = "第"+(num)+"条数据,小区错误！";
                String msg = "<br/>" + returnMsg +  " 导入失败：";
                successMsg.append(msg);
                continue ;
//                user.setAreaName("空");
            }
            if(user.getGroupName().isEmpty()){
                user.setGroupName("空");
            }
            if(user.getUnitName().isEmpty()){
                user.setUnitName("空");
            }
            if(user.getDoorName().isEmpty()){
                user.setDoorName("空");
            }
            if(user.getSpace()==null){
                user.setSpace(0d);
            }
            Boolean purposeSts = false;
            for (HousePurposeT vo : purposeList) {
                if ((vo.getPurposeName()).equals(user.getPurposeName())) {
                    user.setPurposeId(vo.getPurposeId());
                    purposeSts = true;
                }
            }
/*            if (!purposeSts) {
                HousePurposeT purposeVO = new HousePurposeT();
                purposeVO.setPurposeName(user.getBuildMoney());
                purposeVO.setPurposeMoney(1.0);
                purposeVO.setBuildMoney(Double.valueOf(user.getBuildMoney()));
                housePurposeTMapper.insertHousePurposeT(purposeVO);
                List<HousePurposeT> purposeList1 = new ArrayList<HousePurposeT>();
                purposeList1 = housePurposeTMapper.selectHousePurposeTList(housePurposeT);
                for (HousePurposeT vo : purposeList1) {
                    if ((vo.getPurposeName()).equals(user.getBuildMoney())) {
                        user.setPurposeId(vo.getPurposeId());
                    }
                }
            }*/
            houseAreaT.setAreaName(user.getAreaName());
            houseAreaT.setCreater(operName);
            areaList = houseAreaTMapper.selectHouseAreaTList(houseAreaT);
            if(areaList.size()==0) {
                houseAreaTMapper.insertHouseAreaT(houseAreaT);
            }
            houseDoorVo.setAreaName(user.getAreaName());
            houseDoorVo.setGroupName(user.getGroupName());
            houseDoorVo.setUnitName(user.getUnitName());
            houseDoorVo.setDoorName(user.getDoorName());
            doorList = houseDoorTMapper.selectHouseDoorTList(houseDoorVo);
            houseGroupT.setAreaName(user.getAreaName());
            houseGroupT.setGroupName(user.getGroupName());
            houseGroupT.setCreater(operName);
            groupList = houseGroupTMapper.selectHouseGroupTList(houseGroupT);
            if(groupList.size()==0) {
                houseGroupTMapper.insertHouseGroupT(houseGroupT);
            }
            houseUnitT.setAreaName(user.getAreaName());
            houseUnitT.setGroupName(user.getGroupName());
            houseUnitT.setUnitName(user.getUnitName());
            houseUnitT.setCreater(operName);
            unitList = houseUnitTMapper.selectHouseUnitTList(houseUnitT);
            if(unitList.size()==0) {
                houseUnitTMapper.insertHouseUnitT(houseUnitT);
            }

            user.setCreater(operName);
            user.setRateType("0");
            user.setCostSts("1");
            user.setTel(user.getTel());
            user.setUserName(user.getUserName());
            user.setBudgetMoney(user.getCostMoney());
            user.setSpace(user.getSpace());
            user.setPrice(Double.valueOf(user.getPurposeName()));
            user.setBalance(user.getCostMoney());
            user.setBillNo(DateUtils.dateTimeNowMsec());
            id++;
            user.setDoorId(Long.valueOf(id));

            if(doorList.size()>0) {
                user.setHouseId(doorList.get(0).getHouseId());
                dataMigrationTMapper.updateHouseDoorTByCost(user);
                dataMigrationTMapper.insertCost(user);
                continue ;
            }else{
                String houseId = (DateUtils.dateTimeNow()).substring(2,4) + String.format("%" + 5 + "d", id).replace(" ", "0");
                user.setHouseId(houseId);
                for(DoorDataT houseDoor:doorList1){
                    if(houseDoor.getAreaName().equals(user.getAreaName())&&houseDoor.getGroupName().equals(user.getGroupName())&&houseDoor.getUnitName().equals(user.getUnitName())&&houseDoor.getDoorName().equals(user.getDoorName())){
                        doorList1.add(user);
                        dataMigrationTMapper.updateHouseDoorTByCost(user);
                        dataMigrationTMapper.insertCost(user);
                        continue ;
                    }
                }
                doorList1.add(user);
                dataMigrationTMapper.insertHouseDoorT(user);
                dataMigrationTMapper.insertCost(user);
            }
            successNum++;
        }
        successMsg.append("<br/> 导入成功");
        return successMsg.toString();
    }


    @Override
    @Transactional
    public String importProjectUser(List<ProjectDataT> userList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("缴费数据不能为空！");
        }
        int successNum = 1;
        int num = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<SysDictData> sysDictDataList = new ArrayList<SysDictData>();
        List<SysDictData> sysDictDataList1 = new ArrayList<SysDictData>();
        SysDictData sysDictData = new SysDictData();
        List<ProjectDataT> projectList1 = new ArrayList<ProjectDataT>();
        sysDictData.setDictType("repair002");
        sysDictDataList = sysDictDataMapper.selectDictDataList(sysDictData);
        sysDictData.setDictType("repair003");
        sysDictDataList1 = sysDictDataMapper.selectDictDataList(sysDictData);

        for (ProjectDataT user : userList) {
            num++;
            for (SysDictData vo : sysDictDataList) {
                if (vo.getDictLabel().equals(user.getProjectType())) {
                    sysDictDataMapper.selectDictData(vo);
                    user.setProjectType(vo.getDictValue());
                    break;
                }
            }
            for (SysDictData vo : sysDictDataList1) {
                if (vo.getDictLabel().equals(user.getProjectSts())) {
                    sysDictDataMapper.selectDictData(vo);
                    user.setProjectSts(vo.getDictValue());
                    break;
                }
            }
            if(user.getProjectId().isEmpty()){
                failureNum++;
                String returnMsg = "第"+(num)+"条数据,工程编号错误！";
                String msg = "<br/>" + returnMsg +  " 导入失败：";
                successMsg.append(msg);
                return successMsg.toString();
            }
            if(user.getCreater().isEmpty()){
                failureNum++;
                String returnMsg = "第"+(num)+"条数据,建项者错误！";
                String msg = "<br/>" + returnMsg +  " 导入失败：";
                successMsg.append(msg);
                return successMsg.toString();
            }
            dataMigrationTMapper.insertProject(user);
            projectList1.add(user);
            successNum++;
        }

        successMsg.append("导入成功");
        return successMsg.toString();
    }

    @Override
    @Transactional
    public String importRepairUser(List<RepairDataT> userList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("缴费数据不能为空！");
        }
        int successNum = 1;
        int num = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<RepairDataT> repairList = new ArrayList<RepairDataT>();
        for(RepairDataT user : userList){
            System.out.println(user.getProjectId());
            user.setPayMoney(user.getRepairMoney()-user.getNoMoney());
            dataMigrationTMapper.insertRepair(user);
            repairList.add(user);
            successNum++;
        }

        successMsg.append("导入成功");
        return successMsg.toString();
    }

}
